﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Net.Mail;
using System.Text;

namespace PICOC.AspNet.View.Services
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ReportBugService
    {
        [OperationContract]
        public void SendReport(String from, String text)
        {
            if (String.IsNullOrWhiteSpace(text))
                return;

            MailMessage mail = new MailMessage();

            mail.To.Add(new MailAddress("draftpicoc@gmail.com","Draft PICOC"));
            mail.From = new MailAddress("draftpicoc@gmail.com", from);

            String bodyOfMail = generateBody(text);

            mail.Subject = "BUG Report";
            mail.Body = bodyOfMail;

            SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("a1f11dce-0e01-4959-a072-e454eceb47bd@apphb.com", "xx4eh7jv");
            smtpClient.Credentials = credentials;

            smtpClient.Send(mail);
        }

        private string generateBody(string text)
        {
            StringBuilder body = new StringBuilder();

            body.Append("Date and Time: ");
            body.Append(DateTime.Now.ToString());

            body.Append("\nMessage:");
            body.Append(text);

            return body.ToString();
        }
    }
}
